package com.tanhua.sso.controller;

import cn.hutool.core.convert.Convert;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.dubbo.pojo.User;
import com.tanhua.sso.service.SmsService;
import com.tanhua.sso.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("users")
public class MyCenterController {

    @Autowired
    private UserInfoController userInfoController;

    @Autowired
    private SmsService smsService;

    @Autowired
    private SmsController smsController;

    @Autowired
    private UserService userService;

    @DubboReference(version = "1.0.0")
    private UserApi userApi;

    /**
     * 上传头像
     *
     * @param file
     * @param token
     * @return
     */
    @PostMapping("header")
    public Object saveLogo(@RequestHeader("Authorization") String token, @RequestParam("headPhoto") MultipartFile file) {
        return this.userInfoController.userLogo(token, file);
    }


    /**
     * 获取验证码
     */
    @PostMapping("phone/sendVerificationCode")
    public Object sendVerificationCode(@RequestHeader("Authorization") String token) {
        Long userId = this.userService.checkToken(token);
        User user = this.userApi.queryById(userId);
        String phone = user.getMobile();
        Map<String, Object> map = new HashMap<>();
        map.put("phone", phone);
        return this.smsController.sendCheckCode(map);
    }

    /*
     * 校验验证码
     * verification Code:验证码
     * */
    @PostMapping("phone/checkVerificationCode")
    public Object checkVerificationCode(@RequestHeader("Authorization") String token, @RequestBody  Map<String, Object> verificationCode) {
        Object code = verificationCode.get("verificationCode");
        Long userId = this.userService.checkToken(token);
        User user = this.userApi.queryById(userId);
        String phone = user.getMobile();
        return this.userService.jiaoYan(phone, Convert.toStr(code));
    }


    /*
    * 获取要保存的手机号码
    * */
    @PostMapping("phone")
    public Object getPhone(@RequestHeader("Authorization") String token, @RequestBody  Map<String, Object> phone){
        Object newPhone = phone.get("phone");
        return this.userService.savePhone(token, newPhone);


    }

}